from midi.serializable import Serializable

class MidiMessage(Serializable): # 基类
    __status: str # 类型

    def __init__(self, status: str) -> None:
        self.__status = status

    def getStatus(self) -> str:
        return self.__status

class ShortMessage(MidiMessage): # 短消息
    NOTE_ON_ID_CODE = "9"
    NOTE_OFF_ID_CODE = "8"
    CONTROLLER_ID_CODE = "b"
    INSTRUMENT_ID_CODE = "c"
    SLIDE_ID_CODE = "d"

    __data1: str
    __data2: str

    def __init__(self, status: str, data1: str, data2: str) -> None:
        super().__init__(status)
        self.__data1 = data1
        self.__data2 = data2

    def getData1(self) -> str:
        return self.__data1

    def getData2(self) -> str:
        return self.__data2
    
    def hex(self) -> str:
        if self.getStatus()[0] == ShortMessage.INSTRUMENT_ID_CODE: # 乐器选择只有一个附加数据
            return self.getStatus()+self.getData1()
        return self.getStatus()+self.getData1()+self.getData2()

class MetaMessage(MidiMessage): # 元消息
    META_MESSAGE_ID_CODE = 'ff'
    CLOSE_TYPE_ID_CODE = '2f'

    __type: str
    __data_length: str
    __data: str
    
    def __init__(self, status: str, type: str, data_length: str, data: str) -> None:
        super().__init__(status)
        self.__type = type
        self.__data_length = data_length
        self.__data = data

    def getType(self) -> str:
        return self.__type
    
    def getDataLength(self) -> str:
        return self.__data_length
    
    def getData(self) -> str:
        return self.__data
    
    def hex(self) -> str:
        return self.getStatus()+self.getType()+self.getDataLength()+self.getData()

class SysexMessage(MidiMessage): # 系统独占消息
    SYSEX_MESSAGE_BEGIN_ID_CODE: str = 'f0'
    SYSEX_MESSAGE_END_ID_CODE: str = 'f7'

    __data: str

    def __init__(self, status: str, data: str) -> None:
        super().__init__(status)

    def getData(self) -> str:
        return self.__data
    
    def hex(self) -> str:
        return SysexMessage.SYSEX_MESSAGE_BEGIN_ID_CODE+self.getData()+SysexMessage.SYSEX_MESSAGE_END_ID_CODE